package com.tencent.web.controller;

import com.github.pagehelper.PageInfo;
import com.tencent.config.IdUtil;
import com.tencent.entity.CheckGroup;
import com.tencent.entity.Pagination;
import com.tencent.entity.Result;
import com.tencent.entity.SetMeal;
import com.tencent.mapper.CheckGroupMapper;
import com.tencent.service.SelMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@RestController
@RequestMapping("/setmeal")
public class SelMealController {
    @Autowired
    private SelMealService selMealService;
    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @RequestMapping ("/add")
    public Result add(@RequestBody SetMeal setMeal, Integer[] checkgroupIds){
        for (int i = 0; i < checkgroupIds.length; i++) {
            System.out.println(checkgroupIds[i]);
        }

        int add = selMealService.add(setMeal, checkgroupIds);


        return  Result.success("success");
    }


    @GetMapping("/findall")
    public List<SetMeal> findAll(){

        return selMealService.findAll();
    }
    @PostMapping("/delectcode")
    public  void delect(String id){
          selMealService.deleteByCode(id);
    }

    @GetMapping("/findbycode")
    public  Result findByCode(Pagination pagination){
        PageInfo<SetMeal> PageInfo = selMealService.findByCode(pagination);
//        Result result = new Result(true, "ok", PageInfo);
        return  Result.success(PageInfo);
    }
    @RequestMapping("/updata")
    public int updataSetmeal(SetMeal setMeal){
        return selMealService.updateSetmeal(setMeal);
    }
    @RequestMapping("/delectid")
    public  int delectybyid(String id){
        return selMealService.deleteByid(id);
    }
@RequestMapping("/findbyid")
    public  SetMeal findbyid(Integer id){
        return  selMealService.findById(id);
    }

    /**
     * 查询检查组
     * @return
     */
    @GetMapping("/findaddcheckGroup")
    public Result findaddcheckGroup(){
        List<CheckGroup> checkGroupList = selMealService.findaddcheckGroup();
        if (checkGroupList != null && checkGroupList.size()>0) {
            return Result.success(checkGroupList);
        }else {
            return Result.error("错误");
        }

    };
    @GetMapping("findcheckgroudbyid")
    public  Result getcheckgroup(String id){
        List<CheckGroup> findcheckgroupbyid = selMealService.findcheckgroupbyid(id);
        return Result.success(findcheckgroupbyid);
    }

}
